<?php

namespace App\Modules\MemberCard\Controllers;

use App\Lib\Utilities;
use App\Modules\MemberCard\Services\CardTypeService;
use App\Modules\MemberCard\Validators\CardTypeControllerValidator;
use Flower\Core\Http\BaseController;
use Flower\Core\Repository\Exceptions\ValidatorException;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;

class CardTypeController extends BaseController
{
    private $cardTypeService;

    public function __construct(CardTypeService $cardTypeService)
    {
        $this->cardTypeService = $cardTypeService;
    }

    public function getRules()
    {
        return CardTypeControllerValidator::validator();
    }

    /**
     * 新增会员卡类
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function createCardType(Request $request)
    {
        $store_id = $request->input('store_id');
        $category = $request->input('category');
        $price = $request->input('price');
        $name = $request->input('name');
        $present = $request->input('present');
        $count = $request->input('count');
        $royalty = $request->input('royalty');
	    // 辅助推荐提成assist_royalty
	    $assistRoyalty = $request->input('assist_royalty',0);
        $service = $request->input('service');
        $discount_way = $request->input('discount_way',1);
        $skin = $request->input('skin',1);
        $is_general = $request->input('is_general',0);
        $card_type = $this->cardTypeService->createCardType($store_id,$category,$price,$name,$present,$count,$royalty,$service,$discount_way,$skin,$is_general,$assistRoyalty);


        $data = [
            'member_card' => $card_type->id
        ];

        return $this->display($data, '添加成功');

    }

    /**
     * 编辑会员卡类
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function editCardType(Request $request)
    {
        $card_type_id = $request->input('card_type_id');
        $category = $request->input('category');
        $price = $request->input('price');
        $name = $request->input('name');
        $present = $request->input('present');
        $count = $request->input('count');
        $royalty = $request->input('royalty');
	    $assistRoyalty = $request->input('assist_royalty');
        $service = $request->input('service');
        $discount_way = $request->input('discount_way',1);
        $skin = $request->input('skin',1);
        $is_general = $request->input('is_general');
        $this->cardTypeService->editCardType($card_type_id,$category,$price,$name,$present,$count,$royalty,$service,$discount_way,$skin,$is_general,$assistRoyalty);



        return $this->success( '操作成功');

    }

    /**
     * 删除会员卡
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function delCardType(Request $request)
    {
        $card_type_id = $request->input('card_type_id');
        $this->cardTypeService->delCardType($card_type_id);
        return $this->success( '操作成功');
    }

    /**
     * 开关会员卡
     * @param Request $request
     * @return mixed
     */
    public function switchCardType(Request $request)
    {

        $card_type_id = $request->input('card_type_id');
        $status = $request->input('status');
        $this->cardTypeService->switchCardType($card_type_id,$status);

        return $this->success('操作成功');

    }

    /**
     * 获取本店所有会员卡类 分页
     * @param Request $request
     * @return mixed
     */
    public function getCardTypeListPage(Request $request)
    {
        $store_id = $request->input('store_id');
        $status = $request->input('status');
        $keywords = $request->input('keywords');
        $pageSize = $request->input('pageSize');
        $card_type_list = $this->cardTypeService->getCardTypeListPage($store_id,$status,$keywords,$pageSize);

        return $this->display($card_type_list, '操作成功');

    }

    /**
     * 获取本店卡类数量
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function getCardTypeNum(Request $request)
    {
        $store_id = $request->input('store_id');

        $data = $this->cardTypeService->getCardTypeNum($store_id);

        return $this->display($data, '查询成功');
    }

    /**
     * 获取卡类关联服务
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\NotFoundException
     * @throws \Flower\Core\Repository\Exceptions\RepositoryException
     */
    public function getCardTypeService(Request $request)
    {
        $card_type_id = $request->input('card_type_id');

        $data = $this->cardTypeService->getCardTypeService($card_type_id);

        return $this->display($data, '查询成功');
    }

    /**
     * 根据会员卡类id获取会员卡类信息
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\NotFoundException
     */
    public function getCardType(Request $request)
    {
        $card_type_id = $request->input('card_type_id');

        $card_type = $this->cardTypeService->getCardType($card_type_id);

        return $this->display($card_type, '操作成功');

    }

    /**
     * 根据会员卡类id获取会员卡类信息 App
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\NotFoundException
     */
    public function getCardTypeApp(Request $request)
    {
        $card_type_id = $request->input('card_type_id');

        $card_type = $this->cardTypeService->getCardTypeApp($card_type_id);

        return $this->display($card_type, '操作成功');

    }
}
